package dao;

import common.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 封装了对 Problem 的增删改查
 * 1. 新增题目
 * 2. 删除题目
 * 3. 查询题目列表
 * 4. 查询题目的描述
 */
public class ProblemDao {
    /**
     * 新增题目
     *
     * @param problem 题目
     */
    public void insert(Problem problem) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 SQL 语句
            String sql = "insert into oj_problem values(null,?,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, problem.getTitle());
            statement.setString(2, problem.getLevel());
            statement.setString(3, problem.getDescription());
            statement.setString(4, problem.getTemplateCode());
            statement.setString(5, problem.getTestCode());
            // 3. 执行 SQL 语句
            int ret = statement.executeUpdate();
            if (ret == 1) {
                System.out.println("插入成功!");
            } else {
                System.out.println("插入失败!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 4. 关闭释放资源
            DBUtil.close(connection, statement, null);
        }
    }

    /**
     * 删除对应id的题目
     *
     * @param id 题目的序号
     */
    public void delete(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 SQL 语句
            String sql = "delete from oj_problem where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            // 3. 执行 SQL 语句
            int ret = statement.executeUpdate();
            if (ret == 1) {
                System.out.println("删除成功!");
            } else {
                System.out.println("删除失败!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 4. 关闭释放资源
            DBUtil.close(connection, statement, null);
        }
    }

    /**
     * 查询题目列表
     *
     * @return 题目列表
     */
    public List<Problem> selectAll() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Problem> list = new ArrayList<>();
        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 SQL 语句
            String sql = "select id,title,level from oj_problem";
            statement = connection.prepareStatement(sql);
            // 3. 执行 SQL 语句
            resultSet = statement.executeQuery();
            // 4. 遍历结果集
            while (resultSet.next()) {
                Problem problem = new Problem();
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                list.add(problem);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 5. 关闭释放资源
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }

    /**
     * 查找对应id的题目
     *
     * @param id 题目序号
     * @return 返回一个题目
     */
    public Problem selectOne(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 拼装 SQL 语句
            String sql = "select * from oj_problem where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            // 3. 执行 SQL 语句
            resultSet = statement.executeQuery();
            // 4. 遍历结果集
            if (resultSet.next()) {
                Problem problem = new Problem();
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                problem.setDescription(resultSet.getString("description"));
                problem.setTemplateCode(resultSet.getString("templateCode"));
                problem.setTestCode(resultSet.getString("testCode"));
                return problem;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 5. 关闭释放资源
            DBUtil.close(connection, statement, resultSet);
        }
        return null;
    }
    public static void main(String[] args) {
        ProblemDao problemDao = new ProblemDao();
        Problem problem = problemDao.selectOne(1);
        System.out.println(problem);
//        Problem problem = new Problem();
//        problem.setId(1);
//        problem.setTitle("两数之和");
//        problem.setLevel("简单");
//        problem.setDescription("给定一个整数数组 nums和一个整数目标值 target，请你在该数组中找出 和为目标值 target 的那两个整数，并返回它们的数组下标。\n" +
//                "你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。\n" +
//                "你可以按任意顺序返回答案。\n" +
//                "示例 1：\n" +
//                "输入：nums = [2,7,11,15], target = 9\n" +
//                "输出：[0,1]\n" +
//                "解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。\n" +
//                "示例 2：\n" +
//                "输入：nums = [3,2,4], target = 6\n" +
//                "输出：[1,2]\n" +
//                "示例 3：\n" +
//                "输入：nums = [3,3], target = 6\n" +
//                "输出：[0,1]\n" +
//                "\n" +
//                "提示：\n" +
//                "2 <= nums.length <= 104\n" +
//                "-109 <= nums[i] <= 109\n" +
//                "-109 <= target <= 109\n" +
//                "只会存在一个有效答案\n" +
//                "进阶：你可以想出一个时间复杂度小于 O(n2) 的算法吗？\n" +
//                "来源：力扣（LeetCode）\n" +
//                "链接：https://leetcode.cn/problems/two-sum\n" +
//                "著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。");
//        problem.setTemplateCode("class Solution {\n" +
//                "    public int[] twoSum(int[] nums, int target) {\n" +
//                "\n" +
//                "    }\n" +
//                "}");
//        problem.setTestCode("    public static void main(String[] args) {\n" +
//                "        Solution solution = new Solution();\n" +
//                "        int[] test1 = {2,7,11,15};\n" +
//                "        int target1 = 9;\n" +
//                "        int[] res1 = solution.twoSum(test1, target1);\n" +
//                "        if (res1.length == 2 && res1[0] == 0 && res1[1] == 1){\n" +
//                "            System.out.println(\"testcase1 OK!\");\n" +
//                "        }else {\n" +
//                "            System.out.println(\"testcase1 FAILED!\");\n" +
//                "        }\n" +
//                "        int[] test2 = {3,2,4};\n" +
//                "        int target2 = 6;\n" +
//                "        int[] res2 = solution.twoSum(test2, target2);\n" +
//                "        if (res2.length == 2 && res2[0] == 1 && res2[1] == 2){\n" +
//                "            System.out.println(\"testcase2 OK!\");\n" +
//                "        }else {\n" +
//                "            System.out.println(\"testcase2 FAILED!\");\n" +
//                "        }\n" +
//                "    }");
//        problemDao.insert(problem);
    }
}